import os
from dotenv import load_dotenv
from app import create_app, db
from app.models import User, Role, Permission, Menu

load_dotenv()

def check_database():
    try:
        app = create_app()
        with app.app_context():
            # 检查数据库连接
            from sqlalchemy import text
            db.session.execute(text('SELECT 1'))
            print("✅ 数据库连接成功！")
            
            # 检查用户表
            users = User.query.all()
            print(f"用户数量: {len(users)}")
            for user in users:
                print(f"  - {user.username} ({user.email}) - 角色: {[role.name for role in user.roles]}")
            
            # 检查角色表
            roles = Role.query.all()
            print(f"角色数量: {len(roles)}")
            for role in roles:
                print(f"  - {role.name} ({role.code}) - 权限: {[perm.code for perm in role.permissions]}")
            
            # 检查权限表
            permissions = Permission.query.all()
            print(f"权限数量: {len(permissions)}")
            for perm in permissions:
                print(f"  - {perm.name} ({perm.code})")
            
            # 检查菜单表
            menus = Menu.query.all()
            print(f"菜单数量: {len(menus)}")
            for menu in menus:
                print(f"  - {menu.title} ({menu.code})")
                
    except Exception as e:
        print(f"❌ 数据库检查失败: {e}")

if __name__ == '__main__':
    check_database() 